<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>双机热备份</title>
  <script src="js/jquery-3.5.1.min.js"></script>
</head>
<style>
  * {
    padding: 0;
    margin: 0;
  }
  body {
    background: url(/image/log.gif)  no-repeat;
    background-size: 100% ,100%;
  }
  .background {
    display: block;
    width: 100%;
    height: 100%;
    opacity: 0.4;
    filter: alpha(opacity=40);
    background: while;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2000;
  }
  .progressBar {
    padding-top: 10px !important;
    border: solid 2px #86a5ad;
    background: white url(/image/loading.gif) no-repeat left ;
    background-position: 12px 12px;
  }
  .progressBar {
    font-size: 16px;
    display: block;
    width: 160px;
    height: 28px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-25%, -50%);
    margin-left: -74px;
    margin-top: -14px;
    padding: 10px 10px 10px 50px;
    text-align: left;
    line-height: 27px;
    font-weight: bold;
    position: absolute;
    z-index: 2001;
  }
  #showLoading {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0%;
    left: 0%;
    background-color: rgba(0, 0, 0, 0.5);
    display: none;
  }
  .box {
    width: 100%;
    height: 100%;
    text-align: center;
    font-size: 30px;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
  }
  .box div {
    padding-top: 20%;
  }
  .box div button {
    width: 100px;
    height: 50px;
    background-color: aqua;
    border-radius: 10px;
    border: 0px;

  }
  .box div .Btn{
    width: 200px;
    height: 80px;
    font-size: 30px;
    color: white;
  }
  .box .FormData {
    width: 300px;
    height: 300px;
    padding-top: 0%;
    background-color: skyblue;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: none;
  }
  .box .FormData p {
    font-size: 16px;
    line-height: 75px;
  }
  .box .FormData p span {
    display: inline-block;
    width: 80px;
    line-height: 40px;
    border-radius: 5px;
    background-color: aliceblue;
  }
  .box .FormData #upload-form #upload {
    padding: 0px 60px;
    line-height: 70px;
  }
  .box .show {
    width: 100%;
    height: 100%;
    padding-top: 0%;
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0%;
    left: 0%;
    display: none;
  }
  .box .show div {
    width: 350px;
    height: 300px !important;
    background-color: white;
    padding-top: 0%;
    position: fixed;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 15px;
  }
  .box .show div p {
    line-height: 150px;
  }
  /* .box .show div p button{
    background-color: rgb(209, 212, 212);
  } */
</style>
<body>
<h1 style="text-align: center;color:#fff;margin-top: 5%">双机热备份</h1>
<div class="box">

  <div>
    <button onclick="upDatas(1)" class="Btn">点我备份</button>
  </div>
  <div>
    <button onclick="upDatas(2)" class="Btn">点我上传</button>
  </div>
  <div class="FormData" id="btn2">
    <form method="post" enctype="multipart/form-data" id="upload-form">
      <input
              type="file"
              name="file"
              id="upload"
              name="upload"
              accept=".doc, .docx ,image/jpg , image/jpeg , .sql"
      />
    </form>
    <p>IP地址<input type="text" name="address" id="address" /></p>
    <p>端口号 <input type="text" name="port" id="port" /></p>
    <p>
          <span onclick="importData(2)">确认</span
          >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <span onclick="noImportData(2)">取消</span>
    </p>
  </div>
  <div class="FormData" id="btn1">
    <p>IP地址<input type="text" name="address1" id="address1" /></p>
    <p>端口号 <input type="text" name="port1" id="port1" /></p>
    <p>
          <span onclick="importData(1)">确认</span
          >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <span onclick="noImportData(1)">取消</span>
    </p>
  </div>
  <div class="show show2">
    <div>
      <p>确认更新数据？</p>
      <p>
        <button
                onclick="confirmUpdate()"
                style="background-color: rgb(107, 187, 224)"
        >
          确认</button
        >&nbsp;
        <button
                onclick="Cancellation()"
                style="background-color: rgb(218, 214, 214)"
        >
          取消
        </button>
      </p>
    </div>
  </div>
</div>
<div id="showLoading">
  <div id="background" class="background"></div>
  <div id="progressBar" class="progressBar">数据更新中，请稍等...</div>
</div>
</body>
<script>
  var loading = $('#showLoading')
  var ajaxbg = $('#background,#progressBar')
  var showText = $('#progressBar')

  // 上传文件
  let upload = document.querySelector('#upload')
  let btn1 = document.querySelector('#btn1')
  let btn2 = document.querySelector('#btn2')
  var show = document.querySelector('.show')
  var show1 = document.querySelector('.show1')
  var show2 = document.querySelector('.show2')
  var Formdata = document.querySelector('.FormData')

  let word = ''
  var address = document.getElementById('address')
  var port = document.getElementById('port')
  var backupsAddress = document.getElementById('address1')
  var backupsPort = document.getElementById('port1')
  var portFiles = document.getElementById('portFiles')
  var tab = false
  var files = ''
  var ee = ''
  upload.onchange = function (e) {
    const file = e.target.files[0] // 文件信息
    // console.log('e', e.target.files[0])
    files = e.target.files[0]
    console.log(upload.value, 'files')
    if (file) {
      const reader = new FileReader()
      reader.readAsText(file) // 将文件读取为文本
      reader.onload = () => {
        // 文件读取完成后的回调
        // show.textContent = reader.result
      }
      reader.onerror = e => {
        console.log(e)
      }
    }
  }
  // 更新
  function confirmUpdate() {
    const form3 = new FormData()
    form3.append('host', address.value)
    form3.append('port', port.value)
    console.log(form3.get('host'), form3.get('port'))
    show2.style.display = 'none'
    showText.text('数据更新中，请稍等...')
    showText.width('160px')
    loading.show()
    const settings3 = {
      async: true,
      crossDomain: true,
      url: '192.168.10.222:18081/backups/recoveryData',
      method: 'POST',
      headers: {},
      processData: false,
      contentType: false,
      mimeType: 'multipart/form-data',
      data: form3
    }
    $.ajax(settings3).done(function (response) {
      console.log(response)
      var resdata = JSON.parse(response)
      if (resdata.code == 200) {
        showText.text('数据更新完成')
        showText.width('110px')
        setTimeout(() => {
          loading.hide()
        }, 1500)
      } else {
        showText.text('数据更新失败')
        showText.width('110px')
        setTimeout(() => {
          loading.hide()
        }, 1500)
      }
    })
  }
  // 更新取消
  function Cancellation() {
    show.style.display = 'none'
  }

  // input form
  function upDatas(e) {
    Formdata.style.display = 'block'
    if (e == 1) {
      btn1.style.display = 'block'
      btn2.style.display = 'none'
      upload.style.display = 'none'
      btn1.style.height = '250px'
    } else if (e == 2) {
      upload.style.display = 'block'
      btn1.style.display = 'none'
      btn2.style.display = 'block'
      btn2.style.height = '300px'
    }
    // if(tab == false)
  }
  // 导入取消
  function noImportData(e) {
    if (e == 1) {
      btn1.style.display = 'none'
    } else if (e == 2) {
      btn2.style.display = 'none'
    }
    event.stopPropagation()
  }
  // 导入
  function importData(e) {
    event.stopPropagation()
    // console.log(e)
    if (e == 1) {
      // 备份
      if (backupsAddress.value && backupsPort.value) {
        showText.text('文件备份中，等待下载完成提示...')
        showText.width("250px")
        loading.show()
        btn1.style.display = 'none'
        const url = `http://127.0.0.1:18081/backups/backupRecover?host=${backupsAddress.value}&port=${backupsPort.value}&username=root&password=root123456`
        window.open(url, '_self')
        setTimeout(() => {
          loading.hide()
          btn1.style.display = 'none'
        }, 2000)
      } else {
        alert('请完善信息')
      }
    } else if (e == 2) {
      const form2 = new FormData()
      form2.append('host', address.value)
      form2.append('port', port.value)
      form2.append('file', files)
      if (address.value && port.value && upload.value) {
        showText.text('文件上传中，请稍等...')
        loading.show()
        const settings2 = {
          async: true,
          crossDomain: true,
          url: '/backups/fileUpload',
          method: 'POST',
          processData: false,
          contentType: false,
          data: form2
        }
        $.ajax(settings2).done(function (response) {
          console.log(response.code)
          var res = response;
          if (res.code === 200) {
            showText.text('文件上传成功')
            ajaxbg.width('110px')
            setTimeout(() => {
              loading.hide()
              btn2.style.display = 'none'
              show2.style.display = 'block'
            }, 2000)
          }
        })
      } else {
        alert('请完善信息')
      }
    }
  }
  function ajax(options) {
    options = options || {}
    var method = (options.type || 'GET').toUpperCase(),
            url = options.url,
            queryString = null
    if (!url) return
    if (options.data) {
      queryString = []
      for (var attr in options.data) {
        queryString.push(attr + '=' + options.data[attr])
      }
      queryString = queryString.join('&')
    }
    if (method === 'GET' && queryString) {
      url += '?' + queryString
      queryString = ''
    }
    var xhr = new XMLHttpRequest()
    xhr.open(method, url, true)
    if (method === 'POST')
      xhr.setRequestHeader(
              'Content-Type',
              'multipart/form-data'
              // 'application/x-www-form-urlencoded'
      )
    xhr.send(queryString)
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          var data = xhr.responseText
          if (options.dataType === 'json') data = JSON.parse(data)
          options.success && options.success(data)
        } else {
          options.error && options.error(xhr.status)
        }
      }
    }
  }
</script>
</html>
